function add_content(chatContentId,userId,userName,content,timeStamp,html){
	document.getElementById('chatTextfield').value='';
	var chatHistory=document.getElementById('chat_history');
	
	var newDiv=document.createElement('div');
	if(chatContentId!=''){
		newDiv.id="chatContent"+chatContentId;
	}
	var nameSpan=document.createElement('span');
	nameSpan.innerHTML=userName+'<input type="hidden" value="userId" />:&nbsp;';
	var contentSpan=document.createElement('span');
	if(html=='true'){
		contentSpan.innerHTML='&nbsp;&nbsp;'+replaceStr(content);
	}else{
		contentSpan.innerHTML='&nbsp;&nbsp;'+content;
	}
	var timeSpan=document.createElement('span');
	timeSpan.innerHTML=timeStamp;
	newDiv.appendChild(nameSpan);
	newDiv.appendChild(timeSpan);
	newDiv.appendChild(document.createElement('br'));
	newDiv.appendChild(contentSpan);
	
	chatHistory.appendChild(newDiv);
	chatHistory.scrollTop = chatHistory.scrollHeight;
}

function addContent(basePath,sessionId,userId,userName){
	var content=document.getElementById('chatTextfield').value;
	coolio.xhrPost({
		url: basePath+'question/ajax_chat',
   		handleAs: "json",
   		content: {
   			content: content,
   			sessionId: sessionId
	    },
		handle:function(data){
			add_content("",userId,userName,content,format_time(new Date()),'false');
		},
		error: function(response, ioArgs){
	    	alert("xhr get failed:");
		    return response;
		}
	});
}

function checkChatContent(basePath,sessionId,userId){
	var latestTime;
	var divs=document.getElementById('chat_history').getElementsByTagName('div');
	for(var i=divs.length-1;i>=0;i--){
		if(divs[i].getElementsByTagName('input')[0].value!=userId){
			latestTime=divs[i].getElementsByTagName('span')[2].innerHTML;
		}
	}
	coolio.xhrGet({
		url: basePath+'question/get_chat_contents',
   		handleAs: "json",
   		content: {
   			sessionId: sessionId,
   			latestTime: latestTime
	    },
		handle:function(data){
			if(data.contents.length>0){
				for(var j=0;j<data.contents.length;j++){
					if(!document.getElementById('chatContent'+data.contents[j].id)){
						add_content(data.contents[j].id, data.contents[j].ownerId,data.contents[j].ownerName,data.contents[j].content,data.contents[j].timeStamp,data.contents[j].html);
					}
				}
			}
		},
		error: function(response, ioArgs){
	    	alert("xhr get failed:");
		    return response;
		}
	});
}

function showVerifyTable(){
	var div = document.getElementById('promptTable');
	div.style.display='';
	div.style.left='50%';
	div.style.top='50%';
	
	mainDiv=document.getElementById('main');
	mainDiv.onmousedown=function(){return false;};
}

function replaceStr(str){
	str = str.replace(/&lt;/g,"<");
	str = str.replace(/&gt;/g,">");
	str = str.replace(/&quot;/g,"\"");
	str = str.replace(/&amp;/g,"&");
	str = str.replace(/&#039;/g,"'");
	return str;
}